﻿using com.guojin.core.io.message;
using com.guojin.mj.net;
using com.guojin.mj.net.message.game;
using com.guojin.mj.net.message.login;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine;

public class LoginController
{
    private static LoginController instance;
    public static LoginController GetInstance()
    {
        if (instance == null)
        {
            instance = new LoginController();
        }
        return instance;
    }

    public void OnReceiveMsg79(string msg, Message data)
    {
        Debug.Log("服务器返回登录错误");
        //PlayerPrefs.DeleteAll();
        //LoginAndShare.Controller.OnClickWXLogin();
    }

    public void OnReceiveMsg710(string msg, Message data)
    {
        LoginRet lr = (LoginRet)data;
        //保存token
        PlayerPrefs.SetString(DefaultKeyConst.VisitorTokenKey, lr.token);
        Debug.Log("服务器返回token:" + lr.token);

        //保存代理等级和房卡数
        GameData.GetInstance().level = lr.level;
        RoomCard.GetInstance().RoomCardCounts = lr.roomCard;
        GameData.GetInstance().userID = lr.id.ToString();

        //这里加上玩家IP
        GameData.GetInstance().avatar = lr.avatar;
        GameData.GetInstance().userIP = lr.ip;
        GameData.GetInstance().userName = lr.name;
        GameRoomModel.GetInstance().RoomID = lr.roomCheckId;
        GameData.GetInstance().isClubManager = lr.IsClubManager;

        CheckActiveScene();
        CheckNetConnectPage();
    }

    public void OnReceiveMsg717(string msg, Message data)
    {
        Debug.Log("服务器返回重复登录");
    }

    private void CheckActiveScene()
    {
        if (SceneManager.GetActiveScene().name == "SHLogin")
        {
            GameObject obj = GameObject.Find("LoginPage(Clone)");
            if (obj)
            {
                GameObject.Find("StaticSource").GetComponent<MainLogic>().ChangeState((int)GameCore.UIState.UIState_SHLoadingState);
            }
        }
        else if (SceneManager.GetActiveScene().name == "SHGameHall" && GameRoomModel.GetInstance().RoomID != null)
        {
            MessageManager.GetInstance().SendMsg76(GameRoomModel.GetInstance().RoomID);
        }
    }

    private  void CheckNetConnectPage()
    {
        if (GameObject.Find("NetConnectPage(Clone)"))
        {
            GameObject.Find("NetConnectPage(Clone)").GetComponent<NetConnectPage>().CloseBtnClick();
            GameObject.Find("StaticSource").GetComponent<WorldTime>().StartSendPingCountDown();    //开始发送心跳包
        }
    }
}
